<template>
  <div class="login">
    <div class="left">
      <!-- 标题区域 -->
      <div class="title">
        <img class="logo" src="@/assets/img/slogo.png" alt />
        <span class="titleName">黑马面面</span>
        <div class="titleLine"></div>
        <span class="titleName2">用户登录</span>
      </div>
      <!-- 输入框区域 -->
      <el-form :model="form" :rules="rules" ref="form" class="form">
        <!-- 手机号输入框 -->
        <el-form-item prop="phone">
          <el-input v-model="form.phone" prefix-icon="el-icon-user" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <!-- 密码输入框 -->
        <el-form-item prop="password">
          <el-input
            show-password
            v-model="form.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- 验证码输入框 -->
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input v-model="form.code" prefix-icon="el-icon-key" placeholder="请输入验证码"></el-input>
            </el-col>
            <el-col :span="8">
              <img
                class="codePic"
                :src="baseUrl+'/captcha?type=login'+'&id='+num"
                alt
                @click="num = Math.random()"
              />
            </el-col>
          </el-row>
        </el-form-item>
        <!-- 条款 -->
        <el-form-item prop="clause">
          <el-checkbox v-model="form.clause">
            <span class="clause">
              我已阅读并同意
              <el-link type="primary">用户协议</el-link>和
              <el-link type="primary">隐私条款</el-link>
            </span>
          </el-checkbox>
        </el-form-item>
        <!-- 登录按钮 -->
        <el-form-item>
          <el-button class="btn" type="primary" @click="toLogin()">登录</el-button>
        </el-form-item>
        <!-- 注册按钮 -->
        <el-form-item>
          <el-button class="btn" type="primary" @click="toRegister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="right">
      <img src="@/assets/img/logo.png" alt />
    </div>
    <register ref="register"></register>
  </div>
</template>

<script>
import register from "./register.vue";
// import layout from "../layout/layout.vue";
import { userLogin } from "@/api/login.js";
import { saveLocal, getLocal } from "@/utils/local.js";
export default {
  name: "login",
  components: {
    register,
  },
  data() {
    return {
      baseUrl: process.env.VUE_APP_URL,
      // 表单的内容
      form: {
        phone: "",
        password: "",
        code: "",
        clause: false,
      },
      // 验证规则
      rules: {
        phone: [
          { required: true, message: "请输入正确的手机号码", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              let reg = /^(13[0-9]|14[57]|15[0-9]|17[067]|18[0-9])\d{8}$/;
              if (reg.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正确的手机号码"));
              }
            },
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入正确的密码", trigger: "blur" },
          { min: 6, max: 12, message: "请输入6-12位的密码", trigger: "blur" },
        ],
        code: [
          { required: true, message: "请输入正确的验证码", trigger: "blur" },
          { min: 4, max: 4, message: "请输入4位字符的验证码", trigger: "blur" },
        ],
        clause: [
          { required: true, message: "勾选后才可以提交哦", trigger: "change" },
          {
            validator(rule, value, callback) {
              if (value) {
                callback();
              } else {
                callback(new Error("请勾选协议哦"));
              }
            },
            trigger: "blur",
          },
        ],
      },
      // 验证码获取的随机数
      num: "",
    };
  },
  created() {
    if (getLocal("token")) {
      this.$router.push("/layout");
    }
  },
  methods: {
    // 全局表单验证
    toLogin() {
      console.log(this);
      this.$refs.form.validate((result) => {
        if (result) {
          userLogin(this.form).then((res) => {
            saveLocal("token", res.data.data.token);
            // 跳转到首页
            this.$message.success("登录成功");
            this.$router.push("/layout");
            console.log(res);
          });
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    },
    // 弹出注册页面
    toRegister() {
      // 子传父
      this.$refs.register.isShow = true;
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  width: 100%;
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;
  justify-content: space-around;
  align-items: center;
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 42px;
    .title {
      display: flex;
      align-items: center;
      .titleName {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
        margin: 0 16px;
      }
      .logo {
        width: 22px;
        height: 17px;
      }
      .titleLine {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
      }
      .titleName2 {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
        margin-left: 16px;
      }
    }
    .form {
      margin-top: 31px;
      .codePic {
        height: 42px;
        width: 100%;
      }
      .el-input__inner {
        height: 45px;
      }
      .el-button {
        width: 100%;
      }
      .el-form-item {
        margin-bottom: 40px;
      }
      .clause {
        font-size: 14px;
        font-family: Microsoft YaHei Regular, Microsoft YaHei Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #999999;
      }
    }
  }
}
</style>